

package cn.gz3c.uitest.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Point;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.lifecycle.LifecycleOwner;

import cn.gz3c.uitest.viewmodel.EditPreviewViewModel;
import cn.gz3c.uitest.Util;

@SuppressLint("ViewConstructor")
public class TrackViewFrameLayout extends FrameLayout {
    private static final int a = Util.dip2px(18.0F);
    private final Context context;
    private int c;
    private int d = -1;
    private final Point e = new Point();
    private long f;
    private EditPreviewViewModel editPreviewViewModel;
    private boolean h = false;

    public TrackViewFrameLayout(Context context, EditPreviewViewModel editPreviewViewModel, int height) {
        super(context);
        this.context = context;
        this.editPreviewViewModel = editPreviewViewModel;
        this.setLayoutParams(new LayoutParams(-1, height));
        this.init();
    }

    public TrackViewFrameLayout(Context context, int var2, EditPreviewViewModel editPreviewViewModel, int var4) {
        super(context);
        this.context = context;
        this.editPreviewViewModel = editPreviewViewModel;
        this.c = var2;
        if (var4 == 0) {
            this.setLayoutParams(new LayoutParams(-1, Util.dip2px(30.0F)));
        } else {
            this.setLayoutParams(new LayoutParams(-1, Util.dip2px(48.0F)));
        }

        this.init();
    }

    private void init() {
        this.editPreviewViewModel.j().observe((LifecycleOwner) this.context, (var1) -> {
            if (com.huawei.hms.audioeditor.ui.common.utils.a.a(var1)) {
                this.h = false;
            }

        });
    }

    public int b() {
        return this.c;
    }

    public void a(int var1) {
        this.d = var1;
    }

    public int a() {
        return this.d;
    }

    @SuppressLint({"MissingPermission", "WrongConstant"})
    public boolean dispatchTouchEvent(MotionEvent var1) {
        int var2;
        if ((var2 = var1.getActionMasked()) != 0) {
            double var4;
            if (var2 != 1) {
                if (var2 == 2 && !this.h && Math.abs((float) this.e.x - var1.getX()) < 20.0F && Math.abs((float) this.e.y - var1.getY()) < 20.0F && System.currentTimeMillis() - this.f > 500L) {
                    BaseTrackView baseTrackView = null;

                    for (int var3 = 0; var3 < this.getChildCount() && !(this.getChildAt(var3) instanceof ImageView); ++var3) {
                        if ((float) ((baseTrackView = (BaseTrackView) this.getChildAt(var3)).m() + a) < var1.getX()) {
                            var4 = (double) (baseTrackView.m() + a);
                            if (baseTrackView.j() + var4 > (double) var1.getX()) {
                                break;
                            }
                        }
                    }

                    if (baseTrackView instanceof WaveTrackView && !baseTrackView.p()) {
                        this.h = true;
                        Vibrator var7;
                        if (com.huawei.hms.audioeditor.ui.common.utils.a.a(this.context, "android.permission.VIBRATE") && (var7 = (Vibrator) this.context.getSystemService("vibrator")) != null) {
                            var7.vibrate(80L);
                        }

                        this.editPreviewViewModel.c(baseTrackView.o());
                        this.getParent().requestDisallowInterceptTouchEvent(true);
                        ((WaveTrackView) baseTrackView).a(true);
                    }
                }
            } else {
                BaseTrackView var8;
                if (Math.abs((float) this.e.x - var1.getX()) < 20.0F && Math.abs((float) this.e.y - var1.getY()) < 20.0F && System.currentTimeMillis() - this.f < 500L) {
                    for (var2 = 0; var2 < this.getChildCount() && !(this.getChildAt(var2) instanceof ImageView); ++var2) {
                        if ((var8 = (BaseTrackView) this.getChildAt(var2)) instanceof WaveTrackView) {
                            ((WaveTrackView) var8).a(false);
                        }

                        if (var8 instanceof WaveMasterTrackView) {
                            break;
                        }

                        if ((float) (var8.m() + a) < var1.getX()) {
                            var4 = (double) (var8.m() + a);
                            if (var8.j() + var4 > (double) var1.getX()) {
                                String var9;
                                if ((var9 = var8.o()) != null && var9.equals(this.editPreviewViewModel.getLdUuid().getValue())) {
                                    this.editPreviewViewModel.setLdUuid("");
                                } else {
                                    this.editPreviewViewModel.setLdUuid(var9);
                                }
                                break;
                            }
                        }
                    }
                } else {
                    for (var2 = 0; var2 < this.getChildCount() && !(this.getChildAt(var2) instanceof ImageView); ++var2) {
                        if ((var8 = (BaseTrackView) this.getChildAt(var2)) instanceof WaveTrackView) {
                            ((WaveTrackView) var8).a(false);
                        }
                    }
                }
            }
        } else {
            this.e.x = (int) var1.getX();
            this.e.y = (int) var1.getY();
            this.f = System.currentTimeMillis();
        }

        return super.dispatchTouchEvent(var1);
    }
}
